using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace Macaw.Wss3.DependencySpiderSolution
{
    public class DependencySpiderFeatureReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// This is the name of the CustomProperty which will be set on the RootWeb of a SiteCollection to indicate that this SiteCollection 
        /// must be processed by the DependencySpider.
        /// </summary>
        public const string DEPENDENCYSPIDERENABLEDKEY = "DependencySpider.Enabled";

        /// <summary>
        /// This is the name of the CustomProperty which will be set on the RootWeb of a SiteCollection with the url of the Site/Web to 
        /// which the report must be written. This may thus be a different SiteCollection.
        /// </summary>
        public const string DEPENDENCYSPIDERREPORTKEY = "DependencySpider.Report";

        /// <summary>
        /// This is the name of the CustomProperty which will be set on the RootWeb of a SiteCollection to indicate that the DependencyMap build
        /// for this SiteCollection must be stored.
        /// </summary>
        public const string DEPENDENCYSPIDERSTOREMAPKEY = "DependencySpider.StoreMap";

        /// <summary>
        /// 
        /// </summary>
        public const string DEPENDENCYSPIDERSPIDERENABLEDKEY = "DependencySpider.SpiderEnabled";

        /// <summary>
        /// 
        /// </summary>
        public const string DEPENDENCYSPIDEREXCLUDERULESKEY = "DependencySpider.Exclude";

        /// <summary>
        /// 
        /// </summary>
        public const string DEPENDENCYSPIDERINCLUDERULESKEY = "DependencySpider.Include";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            Trace.WriteLine(this.GetType().FullName + ": FeatureActivated - start"); 
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;

            using (SPWeb web = site.OpenWeb(site.RootWeb.ID))
            {
                web.Properties[DEPENDENCYSPIDERENABLEDKEY] = bool.TrueString;
                web.Properties[DEPENDENCYSPIDERREPORTKEY] = null;
                web.Properties[DEPENDENCYSPIDERSTOREMAPKEY] = bool.TrueString;
                web.Properties[DEPENDENCYSPIDEREXCLUDERULESKEY] = null;
                web.Properties[DEPENDENCYSPIDERINCLUDERULESKEY]= null;
                web.Properties[DEPENDENCYSPIDERSPIDERENABLEDKEY]= null;
                web.Properties.Update();
            }
            Trace.WriteLine(this.GetType().FullName + ": FeatureActivated - end");
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            Trace.WriteLine(this.GetType().FullName + ": FeatureDeactivating - start"); 
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;

            using (SPWeb web = site.OpenWeb(site.RootWeb.ID))
            {
                web.Properties.Remove(DEPENDENCYSPIDERENABLEDKEY);
                web.Properties.Remove(DEPENDENCYSPIDERREPORTKEY);
                web.Properties.Remove(DEPENDENCYSPIDERSTOREMAPKEY);
                web.Properties.Remove(DEPENDENCYSPIDEREXCLUDERULESKEY);
                web.Properties.Remove(DEPENDENCYSPIDERINCLUDERULESKEY);
                web.Properties.Remove(DEPENDENCYSPIDERSPIDERENABLEDKEY);
                web.Properties.Update();
            }

            DependencyMapStorageCollection storagecollection = new DependencyMapStorageCollection(site.WebApplication);
            storagecollection.Remove(site.ID);

            Trace.WriteLine(this.GetType().FullName + ": FeatureDeactivating - end"); 
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
